package com.wyx.bbs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wyx.bbs.domain.ArticleToUser;
import com.wyx.bbs.domain.Comment;
import com.wyx.bbs.domain.CommentToUser;
import com.wyx.bbs.domain.User;
import com.wyx.bbs.service.CommentService;
import com.wyx.bbs.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 评论
 * @Date: Created in 16:36 2021/4/1
 * @Modefied By:
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
CommentService commentService;


Map map = new HashMap();
private Object resultMap(int code, String msg){
    map.put("code",code);
    map.put("msg",msg);
    return map;
}

/**
 * 添加
 * @return
 */
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Object addComment (@RequestBody Comment comment){

    int parentId = comment.getParentId();
    System.out.println("parentId:"+parentId);
    int isComment = commentService.addComment(comment);
    if(isComment!=0){

        resultMap(200, "添加成功");
    }

    return map;
    }

/**
 * 根据postid查看
 */
@RequestMapping("/getByPostId")
public Object getByPostId (@RequestParam Integer postId, @RequestParam Integer uid){
    List<CommentToUser> allComments = commentService.getByPostId(postId, uid);

    if (allComments == null || allComments.size() == 0) {
        return new ArrayList<>();
    }


    List<CommentToUser> comments = new ArrayList<>();
    List<CommentToUser> parents = new ArrayList<>();
    List<CommentToUser> parentsd = new ArrayList<>();



    for (CommentToUser comment : allComments) {

        User user = new User();
        user.setAvatarUrl(comment.getAvatarUrl());
        user.setOpenId(comment.getOpenId());
        user.setNikeName(comment.getNikeName());
        user.setId(comment.getUserId());


        if (comment.getParentId() == 0) {
            System.out.println("commentst：" + comment.getParentId());
            System.out.println("commentsId：" + comment.getId());


            comment.setUser(user);

            comments.add(comment);
            parents.add(comment);
        }else{
//            if(comment.getParentId() != 0) {

                for (CommentToUser parent : parents) {
                    System.out.println(JSON.toJSONString(parents)+"________102");
                    System.out.println(parent.getId()+"____________93");

                    comment.setUser(user);

                    if(parent.getUserId() == user.getId()){
                        comment.setParentNikeName(user.getNikeName());
                    }

                    if(comment.getParentId()==parent.getId()){  //子元素



                        if (parent.getChild() == null) {
                            parent.setChild(new ArrayList<>());
                        }


                        parent.getChild().add(comment);
                    }else if(parent.getChild()!=null){
                        for(int i = 0; i < parent.getChild().size(); i ++){
                            if(parent.getChild().get(i).getId()==comment.getParentId()){ //子子评论
                                System.out.println("1111111111111111111111111111---"+comment.getId());
                                parent.getChild().add(comment);
                            }
                        }
                    }


                }

//            }
        }

    }

    return comments;
}


/**
 * 获取评论列表
 */
@RequestMapping("/getCommentList")
public Object getCommentList (@RequestParam Integer uid){
    List<Comment> getComment = commentService.getByUid(uid);
    List<Comment> result = new ArrayList<>();
    for(Comment c: getComment){
        if(c.getUserId()!=uid){
            result.add(c);
        }
    }
    map.put("code",200);
    map.put("list",result);
    return map;
}

/**
 * 读取评论
 */
@RequestMapping("/readComment")
public Object readComment (@RequestBody JSONArray array){
    List list = JSONArray.parseArray(array.toJSONString());
    int readed = commentService.readComment(list);
//    List<Comment> getComment = commentService.getByUid(uid);
//    map.put("code",200);
//    map.put("list",getComment);
    return map;
}


}
